package com.xiaolin.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaolin.dao.VideoMapper;
import com.xiaolin.dao.VideoRepository;
import com.xiaolin.po.Video;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VideoSerivce {

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private VideoRepository videoRepository;

    /**
     * 获取数据库总页数
     *
     * @return
     */
    public Integer getSumPage() {
        PageHelper.startPage(1, 5000);
        List<Video> videos = videoMapper.selectAll();
        System.out.println("第一页数量: " + videos.size());
        if(videos.size() <= 0){
            System.out.println("无数据,退出!");
            System.exit(0);
        }
        PageInfo<Video> videoPageInfo = new PageInfo<>(videos);
        return videoPageInfo.getPages();
    }

    /**
     * 保存到ES
     *
     * @param page
     */
    public void save(Integer page) {
        PageHelper.startPage(page, 5000);
        List<Video> videos = videoMapper.selectAll();
        if(videos.size() <= 0){
            System.out.println(page + "页无数据!");
            return;
        }
        videoRepository.saveAll(videos);

    }

    /**
     * 保存视频
     *
     * @param video
     */
    public int insert(Video video) {
        return videoMapper.insert(video);
    }

    /**
     * 获取最大值Id
     *
     * @return
     */
    public Integer getMax() {
        Integer integer = videoMapper.maxId();
        if (integer == null) {
            integer = 0;
        }
        return integer;
    }


}
